const Controller = require('egg').Controller;

class ClassifyController extends Controller {
  /**
     * 
     * @api {POST} /api/classify/create 创建分类
     * @apiName 创建分类
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} name 分类名
     * 
     * @apiParamExample  {type} Request-Example:
        {
            "name":"前端"
        }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 1,
                "name": "前端",
                "projectId": 1,
                "updatedAt": "2021-11-24T02:31:31.990Z",
                "createdAt": "2021-11-24T02:31:31.990Z"
            }
        }
     * 
     * 
     */
  async create() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(this, 'name').checkDataRes;

    try {
      if (checkRes) {
        let res = await service.web.classify.create();
        if (res == '分类名已存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     * 
     * @api {DELETE} /api/classify/delete 删除分类
     * @apiName 删除分类
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} id 分类id
     *  
     * @apiParamExample  {type} Request-Example:
        {
            "id":2
        }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     * 
     * 
     */
  async delete() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;

    try {
      if (checkRes) {
        let res = await service.web.classify.delete();
        if (res == 'id不存在,删除失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
    * 
    * @api {PUT} /api/classify/update 修改分类
    * @apiName 修改分类
    * @apiGroup 分类模块
    * @apiVersion  1.0.0
    * 
    * 
    * @apiParam  {Number} id 案例id
    * @apiParam  {String} name  分类名称
    * 
    * 
    * @apiParamExample  {type} Request-Example:
        {
            "id":1,
            "name":"哈哈"
        }
    * 
    * 
    * @apiSuccessExample {type} Success-Response:
       {
           "code": 200,
           "msg": "修改成功",
           "data": []
       }
    * 
    * 
    */
  async update() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(this, 'id', 'name').checkDataRes;

    try {
      if (checkRes) {
        let res = await service.web.classify.update();
        if (res == 'id不存在,修改失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     * 
     * @api {GET} /api/classify/getAll?limit=10^offset=0 获取所有分类
     * @apiName 获取所有分类
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} limit 分类返回数
     * @apiParam  {Number} offset 分类偏移数
     * 
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 2,
                "rows": [
                    {
                        "id": 1,
                        "name": "哈哈",
                        "projectId": 1,
                        "createdAt": "2021-11-24T02:31:31.000Z",
                        "updatedAt": "2021-11-24T02:55:09.000Z"
                    },
                    {
                        "id": 3,
                        "name": "后台",
                        "projectId": 1,
                        "createdAt": "2021-11-24T03:05:35.000Z",
                        "updatedAt": "2021-11-24T03:05:35.000Z"
                    }
                ],
                "projectName": "官网项目"
            }
        }
     * 
     * 
     */
  async getAll() {
    const { app, ctx, service } = this;
    let res = await service.web.classify.getAll();
    return ctx.helper.successModule(res);
  }

  /**
     * 
     * @api {GET} /api/classify/getById?id=1 根据分类id查找分类下所有案例
     * @apiName 根据分类id查找分类下所有案例
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} id 分类id
     * 
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 1,    //分类id
                "name": "哈哈", //分类名
                "projectId": 1, //所属项目id
                "createdAt": "2021-11-24T02:31:31.000Z",
                "updatedAt": "2021-11-24T02:55:09.000Z",
                "Cases": [
                    {
                        "id": 72,   //案例id
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //案例封面图
                        "name": "case", //案例名
                        "sketch": "sketch", //案例描述
                        "content": "content",   //案例内容
                        "characterId": 1,   //人物id
                        "projectId": 1, 
                        "createdAt": "2021-11-24T03:24:13.000Z",
                        "updatedAt": "2021-11-24T03:24:13.000Z",
                        "CaseClassify": {
                            "id": 1,
                            "caseId": 72,
                            "classifyId": 1,
                            "createdAt": "2021-11-24T03:24:13.000Z",
                            "updatedAt": "2021-11-24T03:24:13.000Z"
                        }
                    },
                    {
                        "id": 73,
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                        "name": "case",
                        "sketch": "sketch",
                        "content": "content",
                        "characterId": 1,
                        "projectId": 1,
                        "createdAt": "2021-11-24T03:24:59.000Z",
                        "updatedAt": "2021-11-24T03:24:59.000Z",
                        "CaseClassify": {
                            "id": 3,
                            "caseId": 73,
                            "classifyId": 1,
                            "createdAt": "2021-11-24T03:24:59.000Z",
                            "updatedAt": "2021-11-24T03:24:59.000Z"
                        }
                    }
                ]
            }
        }
     * 
     * 
     */
  async getById() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;
    try {
      if (checkRes) {
        let res = await service.web.classify.getById();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      return ctx.helper.errorModule(error.message);
    }
  }
}
module.exports = ClassifyController;
